package band.wukong.practice.util.mess;

import java.sql.*;

/**
 * <P>Title: band.wukong.practice.util.JdbcUtils</P>
 * <p>Description: oracle jdbc工具类</p>
 * <p>Date: 2009-5-7 9:46:17 </p>
 *
 * @author sunjs
 */
public final class JdbcUtils {
    //    private static final String DB_DRIVER_MYSQL = "com.mysql.jdbc.Driver";
    private static final String DB_DRIVER_ORACLE = "oracle.jdbc.driver.OracleDriver";

    /**
     * 得到jdbc的Connection
     *
     * @param url      url，包括scheme
     * @param name     数据库用户名
     * @param password 数据库密码
     * @param dbDriver 数据库名称
     * @return
     * @throws RuntimeException
     */
    public static Connection getConnection(String url, String name, String password, String dbDriver) {
//        String url = "jdbc:oracle:thin:@10.10.3.237:1521:tarena";
//        String name = "openlab";
//        String password = "open123";
        try {
            Class.forName(dbDriver);
            return DriverManager.getConnection(url, name, password);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 关闭PreparedStatement和ResultSet
     *
     * @param pstm
     * @param rs
     * @throws RuntimeException
     */
    public static void closePreparedStatementAndResultSet(PreparedStatement pstm, ResultSet rs) {
        try {
            if (null != rs)
                rs.close();
            if (null != pstm)
                pstm.close();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static void closeConnection(Connection conn) {
        if (null != conn) {
            try {
                conn.close();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }

    /**
     * 给传入的sql分页
     *
     * @param currPage 当前页
     * @param pageSize 每页记录数【必备，非零值】
     * @param sql      原始sql【必备,非空值】
     * @return 分页sql
     * @throws IllegalArgumentException 当参数不符合规范时抛出
     */
    public static String genDivePageSql4oracle(int currPage, int pageSize, String sql) {
        if (pageSize == 0 || null == sql || sql.equals("")) {
            throw new IllegalArgumentException("参数不合规范，请检查参数值");
        }

        int nMin = 0;
        int nMax = pageSize;
        if (currPage > 0) {
            nMin = ((currPage - 1) * pageSize);
            nMax = currPage * pageSize;
        }
        StringBuffer szSql = new StringBuffer();
        szSql.append("SELECT A.* FROM (\n");
        szSql.append("  SELECT B.* , rownum as my_rownum\n");
        szSql.append("  FROM (\n");
        szSql.append("      ").append(sql).append("\n");
        szSql.append("  ) B \n");
        szSql.append("  where rownum <= ").append(nMax);
        szSql.append(") A \n");
        szSql.append("WHERE my_rownum > ").append(nMin);

        return szSql.toString();
    }

    /**
     * 打印ResultSet
     *
     * @param rs
     */
    public static void printResultSet(ResultSet rs) {
        ResultSetMetaData rsmd = null;
        int colCount = 0;
        try {
            rsmd = rs.getMetaData();
            colCount = rsmd.getColumnCount();

            while (rs.next()) {
                for (int i = 1; i <= colCount; i++) {
                    if (i > 1) {
                        System.out.print(",");
                    }
                    String name = rsmd.getColumnName(i);
                    String value = rs.getString(i);
                    System.out.print(name + "=" + value);
                }
                System.out.println();
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }

}
